package com.hyt.it.ogt.kq.main.controller.gov;


import com.hyt.common.apiInfor.ApiResponse;
import com.hyt.common.apiInfor.DataType;
import com.hyt.common.apiInfor.ParamType;
import com.hyt.it.ogt.kq.common.gov.constant.ResponseCode;
import com.hyt.it.ogt.kq.main.controller.base.BaseController;
import com.hyt.it.ogt.kq.common.config.exception.KqException;
import com.hyt.it.ogt.kq.service.gov.model.param.OfficeFileTemplateParam;
import com.hyt.it.ogt.kq.service.gov.service.*;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.validation.Valid;


/**
 * <p>
 * 模板文件参数表 前端控制器
 * </p>
 *
 * @author huangyh
 * @since 2021-09-02
 */
@RestController
@RequestMapping("/tk/officeTemplateData")
@Api(tags = "117.模板数据接口", value = "模板数据接口")
@ApiSort(value = 117)
@Slf4j
public class OfficeTemplateDataController extends BaseController {

    @Resource
    private IOfficeFileTemplateService iOfficeFileTemplateService;

    @Resource
    private ITaskService iTaskService;

    @Resource
    private ISysAreaService iSysAreaService;

    @Resource
    private IOfficeCandidateAreaService iOfficeCandidateAreaService;

    @Resource
    private IOfficeAreaService iOfficeAreaService;
    @Resource
    private ITaskCandidateAreaService iTaskCandidateAreaService;
    @Resource
    private IOfficeExamAgreementService iOfficeExamAgreementService;

    @ApiOperation(value = "17.1 新增违纪基础数据模板")
    @ApiOperationSort(value = 1)
    @PostMapping(value = "/add")
    public ApiResponse<Object> add(@RequestParam MultipartFile file, @Valid OfficeFileTemplateParam param) {
        try {
            iOfficeFileTemplateService.add(file, param);
            return ApiResponse.builder()
                    .code(ResponseCode.SUCCESS.getCode())
                    .build();
        } catch (KqException k) {
            log.error("# 新增违纪基础数据模板业务异常: {}", k.getErrMsg());
            return ApiResponse.builder()
                    .code(k.getErrCode())
                    .data(k.getErrMsg())
                    .build();
        } catch (Exception e) {
            log.error("# 新增模板异常: ", e);
            return ApiResponse.builder()
                    .code(ResponseCode.ERROR_ADD_OFFICE_FILE_TEMPLATE.getCode())
                    .build();
        }
    }

    @ApiOperation(value = "17.2 任务数据初始化(所有)")
    @ApiOperationSort(value = 2)
    @PostMapping(value = "/initTaskData")
    public ApiResponse<Object> initTaskData(String taskId, String officeId) {
        try {
            //初始化模板参数
            iTaskService.initTaskData(taskId,officeId);
            return ApiResponse.builder()
                    .code(ResponseCode.SUCCESS.getCode())
                    .build();
        } catch (KqException k) {
            log.error("# 任务数据初始化(所有)业务异常: {}", k.getErrMsg());
            return ApiResponse.builder()
                    .code(k.getErrCode())
                    .data(k.getErrMsg())
                    .build();
        } catch (Exception e) {
            log.error("# 任务数据初始化(所有)异常: ", e);
            return ApiResponse.builder()
                    .code(ResponseCode.TASK_INIT_FAIL.getCode())
                    .build();
        }
    }

    @ApiOperation(value = "17.3 系统区域导入(excel)")
    @ApiOperationSort(value = 3)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "initType", value = "机构初始化类型：1.不初始化，2.初始化", dataType = DataType.STRING,
                    paramType = ParamType.QUERY, required = true)
    })
    @PostMapping(value = "/importSysArea")
    public ApiResponse<Object> importSysArea(MultipartFile file,String initType) {
        try {
            //系统区域导入，如果存在则不导入，除非删除
            iSysAreaService.importSysArea(file,initType);
            return ApiResponse.builder()
                    .code(ResponseCode.SUCCESS.getCode())
                    .build();
        } catch (KqException k) {
            log.error("# 系统区域导入(excel)业务异常: {}", k.getErrMsg());
            return ApiResponse.builder()
                    .code(k.getErrCode())
                    .data(k.getErrMsg())
                    .build();
        } catch (Exception e) {
            log.error("# 系统区域导入(excel)异常: ", e);
            return ApiResponse.builder()
                    .code(ResponseCode.ERROR_INIT_SYS_AREA.getCode())
                    .build();
        }
    }

    @ApiOperation(value = "17.4 机构考生区域导入(excel)")
    @ApiOperationSort(value = 4)
    @PostMapping(value = "/importOfficeCandidateArea")
    public ApiResponse<Object> importOfficeCandidateArea(MultipartFile file, String officeId) {
        try {
            if (StringUtils.isEmpty(officeId)) {
                return ApiResponse.builder()
                        .code(ResponseCode.ERROR_INIT_CANDIDATE_OFFICE_AREA.getCode())
                        .build();
            }
            //机构考生区域导入，如果存在则不导入，除非删除
            iOfficeCandidateAreaService.importOfficeCandidateArea(file, officeId);
            return ApiResponse.builder()
                    .code(ResponseCode.SUCCESS.getCode())
                    .build();
        } catch (KqException k) {
            log.error("# 机构考生区域导入(excel)业务异常: {}", k.getErrMsg());
            return ApiResponse.builder()
                    .code(k.getErrCode())
                    .data(k.getErrMsg())
                    .build();
        } catch (Exception e) {
            log.error("# 机构考生区域导入(excel)异常: ", e);
            return ApiResponse.builder()
                    .code(ResponseCode.ERROR_INIT_CANDIDATE_OFFICE_AREA.getCode())
                    .build();
        }
    }

    @ApiOperation(value = "17.5 机构区域导入(excel)")
    @ApiOperationSort(value = 5)
    @PostMapping(value = "/importOfficeArea")
    public ApiResponse<Object> importOfficeArea(MultipartFile file, String officeId) {
        try {
            if (StringUtils.isEmpty(officeId)) {
                return ApiResponse.builder()
                        .code(ResponseCode.ERROR_INIT_PLACE_ROOM_OFFICE_AREA.getCode())
                        .build();
            }
            //考点考场所属机构区域初始化,如果存在则不初始化，除非删除
            iOfficeAreaService.importOfficeArea(file, officeId);
            return ApiResponse.builder()
                    .code(ResponseCode.SUCCESS.getCode())
                    .build();
        } catch (KqException k) {
            log.error("# 机构区域导入(excel)业务异常: {}", k.getErrMsg());
            return ApiResponse.builder()
                    .code(k.getErrCode())
                    .data(k.getErrMsg())
                    .build();
        } catch (Exception e) {
            log.error("# 机构区域导入(excel)异常: ", e);
            return ApiResponse.builder()
                    .code(ResponseCode.ERROR_INIT_PLACE_ROOM_OFFICE_AREA.getCode())
                    .build();
        }
    }

    @ApiOperation(value = "17.6 违纪机构数据初始化(excel)")
    @ApiOperationSort(value = 6)
    @PostMapping(value = "/initOfficeBreakRule")
    public ApiResponse<Object> initOfficeBreakRule(@RequestParam MultipartFile file, String officeId) {
        try {
            //违纪机构数据初始化,如果存在则不初始化，除非删除

            return ApiResponse.builder()
                    .code(ResponseCode.SUCCESS.getCode())
                    .build();
        } catch (KqException k) {
            log.error("# 违纪机构数据初始化(excel)业务异常: {}", k.getErrMsg());
            return ApiResponse.builder()
                    .code(k.getErrCode())
                    .data(k.getErrMsg())
                    .build();
        } catch (Exception e) {
            log.error("# 违纪机构数据初始化(excel)异常: ", e);
            return ApiResponse.builder()
                    .code(ResponseCode.ERROR_INIT_OFFICE_BREAK_RULE.getCode())
                    .build();
        }
    }

    @ApiOperation(value = "17.7 一键清除机构模板数据")
    @ApiOperationSort(value = 7)
    @PostMapping(value = "/clean")
    public ApiResponse<Object> clean(String officeId) {
        try {
            iTaskService.cleanOfficeData(officeId);
            return ApiResponse.builder()
                    .code(ResponseCode.SUCCESS.getCode())
                    .build();
        } catch (KqException k) {
            log.error("# 一键清除机构模板数据业务异常: {}", k.getErrMsg());
            return ApiResponse.builder()
                    .code(k.getErrCode())
                    .data(k.getErrMsg())
                    .build();
        } catch (Exception e) {
            log.error("# 一键清除机构模板数据异常: ", e);
            return ApiResponse.builder()
                    .code(ResponseCode.DELETE_OFFICE_INIT_FAIL.getCode())
                    .build();
        }
    }

    @ApiOperation(value = "17.8 新增违纪基础数据模板")
    @ApiOperationSort(value = 8)
    @PostMapping(value = "/add2")
    public ApiResponse<Object> add2(@RequestParam MultipartFile file, String taskId) {
        try {
            String officeId = getOfficeId();
            iOfficeFileTemplateService.add2(file, taskId,officeId);
            return ApiResponse.builder()
                    .code(ResponseCode.SUCCESS.getCode())
                    .build();
        } catch (KqException k) {
            log.error("# 新增违纪基础数据模板业务异常: {}", k.getErrMsg());
            return ApiResponse.builder()
                    .code(k.getErrCode())
                    .data(k.getErrMsg())
                    .build();
        } catch (Exception e) {
            log.error("# 新增模板异常: ", e);
            return ApiResponse.builder()
                    .code(ResponseCode.ERROR_ADD_OFFICE_FILE_TEMPLATE.getCode())
                    .build();
        }
    }

    @ApiOperation(value = "17.9 任务-考生区域导入（报名版）(excel)")
    @ApiOperationSort(value = 9)
    @PostMapping(value = "/importCandidateAreaByBm")
    public ApiResponse<Object> importCandidateAreaByBm(MultipartFile file, String taskId) {
        try {
            if (StringUtils.isEmpty(taskId)) {
                return ApiResponse.builder()
                        .code(ResponseCode.ERROR_INIT_CANDIDATE_TASK_AREA_BM.getCode())
                        .build();
            }
            //机构考生区域导入，如果存在则不导入，除非删除
            iTaskCandidateAreaService.importCandidateAreaByBm(file, taskId);
            return ApiResponse.builder()
                    .code(ResponseCode.SUCCESS.getCode())
                    .build();
        } catch (KqException k) {
            log.error("# 机构考生区域导入报名版(excel)异常: {}", k.getErrMsg());
            return ApiResponse.builder()
                    .code(k.getErrCode())
                    .data(k.getErrMsg())
                    .build();
        } catch (Exception e) {
            log.error("# 机构考生区域导入报名版(excel)异常: ", e);
            return ApiResponse.builder()
                    .code(ResponseCode.ERROR_INIT_CANDIDATE_TASK_AREA_BM.getCode())
                    .build();
        }
    }

    @ApiOperation(value = "17.10 初始化系统级考生须知模板")
    @ApiOperationSort(value = 10)
    @PostMapping(value = "/importExamNotice")
    public ApiResponse<Object> importExamNotice(MultipartFile file) {
        try {

            iOfficeExamAgreementService.importExamNotice(file);

            return ApiResponse.builder()
                    .code(ResponseCode.SUCCESS.getCode())
                    .build();
        } catch (KqException k) {
            log.error("# 初始化系统级考生须知模板异常: {}", k.getErrMsg());
            return ApiResponse.builder()
                    .code(k.getErrCode())
                    .data(k.getErrMsg())
                    .build();
        } catch (Exception e) {
            log.error("# 初始化系统级考生须知模板异常: ", e);
            return ApiResponse.builder()
                    .code(ResponseCode.ERROR_IMPORT_EXAM_NOTICE.getCode())
                    .build();
        }
    }

}
